<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> 
<head>
<%@ include file="/WEB-INF/jsp/include/easyui.jsp" %>
<%@ include file="/WEB-INF/jsp/include/pGrid.jsp" %>
<%@ include file="/WEB-INF/jsp/include/pForm.jsp" %>
<title>角色菜单动作设置</title>
<style type="text/css">
.allMotion{color: black;cursor: pointer;}
</style>
<script>
	$(function(){
		/*标记列表为树形*/
		p.isTreeGrid = true;
		p.dataGrid = $(p.dataGrid).treegrid($.extend({},p.dataGridParam,{
			url: "/console/com.fs.base.pojo.Menu/list" ,
			columns: [[
		         {field:"menuName",width:180,title:"菜单名称" ,formatter:function(value,row,i){
		        	 if(row.children){
		        		 return value;
		        	 }else{
			        	 return value + '&nbsp; <a class="allMotion" menuid="'+ row.id +'" href="javascript:void(0)" >全选</a>'; 
		        	 }
		         }},
		         {field:'dataRight', width:140, title:'数据权限', formatter:function(value,row,i){
		        	 if(!row.children){
		        		 return '<select name="dataRight"  menuid="'+ row.id +'" style="width:130px;"><option value="1">自己的数据</option><option value="4">所有的数据</option><option value="2">所属组织机构数据</option><option value="3">组织机构及下级机构数据</option><option value="5">所属项目部数据</option></select>';
		        	 }
		         }},
		         {field:"menuMotion",title:"动作配置列表" ,formatter:function(value,row,i){
		        	 /*拉去动作列表*/
		        	 $.ajaxSettings.async = false;
		        	 var motions = [],menuId = row.id;
		        	 var param = { menuId: menuId, menuId_searchType: "eq",sort:'motionOrder',order:'asc' };
		        	 $.getJSON("/console/com.fs.base.pojo.MenuMotion/list", param ,function(data){
		        		 $.each(data.rows,function(idx,m){
		        			var menuMotionName = m.id ;
		        			var motionBox = '<input class="motionBox" type="checkbox"  menuid="'+ menuId +'"  name="menuMotion"  value="'+ menuMotionName +'"  />'; 
		        			motions.push(motionBox);
		        			motions.push(m.motionCode +  m.motionName);
		                 });
		        	 });
		        	 $.ajaxSettings.async = true;
		        	 return motions.join(" ");
		         }}
			]],
			lines: true,
			idField: "treeId", 
			treeField: "menuName",
			singleSelect: false, 
			pagination: false,
			queryParams:{isTreeGrid:"true", treeId:"id", treePid:"parentId" },
			checkbox:function(row){
				return true;
			},
			onCheckNode:function(row,checked){
				allCheck(row,checked);
				function allCheck(node,checked){
					var menuid = node.id;
					if(checked==false){
						$("input[menuid="+ menuid +"]").removeAttr("checked");  
					}else{
						var len = $(".motionBox[menuid="+ menuid +"]").serializeArray().length;
						if(len==0){
							$("a[menuid="+ menuid +"]").click();
						}
					}
					node.children = node.children || [];
					var len = node.children.length;
					if(len>0){
						$.each(node.children, function(i, n){ 
							allCheck(n,n.checked);
						});
					}
				}
			},
			onLoadSuccess:function(data){
				$(".allMotion").on("click",function(){
					var menuid = $(this).attr("menuid");
					$("input[menuid="+ menuid +"]").each(function () {  
			            this.checked = true;  
			         });
					var treeBox = $("tr[node-id="+ menuid +"]").find(".tree-checkbox");
					if(!treeBox.hasClass("tree-checkbox1")) treeBox.click();
				});
				//取消选择动作冒泡
				$(".motionBox").on("click",function(e){
					var $this = $(this),menuid = $this.attr("menuid");
					var checked = $this.is(":checked") ;
					var treeBox = $("tr[node-id="+ menuid +"]").find(".tree-checkbox");
					if(checked==true){
						if(!treeBox.hasClass("tree-checkbox1")) treeBox.click();
					}else{
						var len = $(".motionBox").serializeArray().length;
						if(len==0 && treeBox.hasClass("tree-checkbox1")) treeBox.click();
					}
					event.stopPropagation();
				});
				
				/*按钮权限回显*/
		 		 $.getJSON("/console/role/getRoleMenuMotions", { roleId : fn.data.id } ,function(roleMenuMotions){
		 			var roleMenuMotions = roleMenuMotions || '';
		 			roleMenuMotions = roleMenuMotions.split("#");
		 			$.each(roleMenuMotions,function(rIndex,menu_motion){
		 				if(menu_motion != '') $("input[value="+ menu_motion +"]").click();
	                }); 
		 		 });
				
		 		/*数据权限回显*/
					$.ajaxSettings.async = true;
					var param = { roleId: fn.data.id ,roleId_searchType:'eq'};
			 		$.getJSON("/console/com.fs.base.pojo.RoleMenuAccess/list", param ,function(data){
			 			$.each(data.rows,function(rIndex,item){
			 				$("select[name=dataRight][menuid="+ item.menuId +"]").find("option[value='"+ item.access +"']").attr("selected",true);
		                }); 
			 		});
			 		$.ajaxSettings.async = false;
			}
		}));
		
		$("#toolbar .easyui-linkbutton").on("click", function() {
			var id = $(this).attr("id");
			switch(id){
				case "b_5": p.gridQuery();break;
				default:break;
			}
		});
		
		fn.resetForm();
		
		/*添加提交前事件*/
		fn.onBeforeSubmitForm = function(){
			var form = $("#easyuiForm").html("");
			fn.queryParams.roleId = fn.data.id
			
			/*数据权限封装*/
			var selections = p.dataGrid.treegrid("getCheckedNodes");
			var ids = [];
			$.each(selections,function(i,node){
				ids.push(node.id);
			});
			ids = ids.join(",");
			var accessJson = [],menuid = null;
			$.each($("select[name=dataRight]"),function(i,item){
				$item = $(item) ,menuid = $item.attr("menuid");
				if(ids.indexOf(menuid)>-1){
					accessJson.push({menuId: $item.attr("menuid"), access: $item.val() ,roleId: fn.data.id });
				}
			});
			fn.queryParams.accessJson = JSON.stringify(accessJson) ;
			
			$("input:checkbox:checked").clone().appendTo(form);
		};
	});
</script>
</head>
<body>
	<div class="easyui-panel" data-options="fit:true,border:false" style="padding:0px;">
		<form id="easyuiForm" method="post" action="/console/role/roleMenuMotionConfig"  style="display: none;" >
			<input type="hidden" name="roleId" value="" />
		</form>	
		<!-- 工具栏 -->
		<div id="toolbar" class="easyui-panel" style="padding:3px;width:100%;">
			<!-- 工具栏按钮  -->
	      	<a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" id="b_5">刷新</a>
		</div>
		<!-- 表格 --> 
		<table id="dataGrid"></table>
	</div>
</body>
</html>